/**
 * desc: .通过http获取外网服务器的数据
 * User : hjt
 * Date : 2017/1/4
 */

//引入hrrp和fs原生模块
var http = require("http");
var fs = require("fs");
var file = require("./file");

//抓取数据url
//var url = "http://www.luoo.net/music/882";
//var url = "http://world.huanqiu.com/exclusive/2017-01/9901347.html";
var url = "http://www.luoo.net/music/882";


//网页的解析---获取想要的数据，进行数据的分析和抓取，找素材
//安装:npm install cheerio --save-dev
//cheerio模仿jquery.   为服务器特别定制的，快速、灵活、实施的jQuery核心实现.
var cheerio = require("cheerio");

//get / post
http.get(url, function(res){
    var html = "";
    //data是请求数据进行中，管道fs网络字迹字节流，data是读取一部分返回的数据字节Buffer对象
    res.on("data", function(data){
        html += data.toString("utf8");
    });

    //如果.on("data") 执行完毕 就会触发 .on("end")的事件
    res.on("end", function(){
        //解析html
        parseHtml(html);
    });

    //出现error
    res.on("error", function(err){
        console.log("read Web Page["+url+ "] error...")
    });
});

//解析html
function parseHtml(html){
    //https://github.com/cheeriojs/cheerio
    var $ = cheerio.load(html);
    //var title = $(".vol-title").text();
    //console.log("===>"+title);
    //
    //var content = $(".vol-desc").html();
    //console.log("===>"+content.toString("utf8"));

    console.log("===>"+$("body"));

    var $imgs = $("body").find("img");
    for(var i=0;i<$imgs.length;i++){
        var src = $($imgs[i]).attr("src");
        if(src.indexOf("img-cdn")!=-1) {
            //path模块
            downloadImage("D:/test/luo/" + (parseInt(Math.random() * 1000)) + ".jpg", src);
        }
    }
}

//下载图片
function downloadImage(pathname, url){
    http.get(url, function(res){
        res.setEncoding("binary");      //二进制
        var imgData = "";
        res.on("data", function(data){
            imgData += data;
        }).on("end", function(data){
            file.writeFileBinary(pathname, imgData).then(function(){
                console.log(url + " download success!");
            }).catch(function(e){
                console.log(url + " download exception!"+e);
            });
        }).on("error", function(){
            console.log( " read error!");
        });
    });
}
